package com.init.iblog.Controller;

import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.entity.Role;
import com.init.iblog.domain.entity.User;
import com.init.iblog.service.RoleService;
import com.init.iblog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     *
     * 分页查询角色列表
     * @param pageNum
     * @param pageSize
     * @param roleName
     * @return
     */
    @GetMapping("/list")
    public ResponseResult getRoleList(Integer pageNum, Integer pageSize,String roleName,String status){
        System.out.println("========getRoleList接口==========");
        return roleService.getRoleList(pageNum,pageSize,roleName,status);
    }


    /**
     * 查询角色列表
     * @param pageNum
     * @param pageSize
     * @param roleName
     * @return
     */
    @GetMapping("/listAllRole")
    public ResponseResult getListAllRole(Integer pageNum, Integer pageSize,String roleName){
        System.out.println("===========getListAllRole接口==========");
        List<Role> list = roleService.list();
        return ResponseResult.okResult(list);
    }


    /**
     * 获取角色信息
     *
     * TODO  存在问题
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getRole(@PathVariable Long id){
        System.out.println("===========进入角色信息接口=========");

        return roleService.getRole(id);
    }

    /**
     * 更新角色
     * @param role
     * @return
     */
    @PutMapping
    public ResponseResult updateRole(@RequestBody Role role){

        System.out.println("=========进入更新角色接口========");

        return roleService.updateRole(role);
    }


    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteRole(@PathVariable List<Long> id){
        System.out.println("===========进入删除角色接口===========");
        return roleService.deleteRole(id);
    }


    /**
     * 添加角色
     * @param role
     *
     * @return
     */
    @PostMapping
    public ResponseResult addRole(@RequestBody Role role){
        System.out.println("==========进入添加角色接口========");
        //获取当前登录角色
        User user = SecurityUtils.getLoginUser().getUser();
        Long create_by = user.getId();
        role.setCreateBy(create_by);
        role.setCreateTime(new Date());
        return roleService.addRole(role);
    }


    @PutMapping("/changeStatus")
    public ResponseResult changeRoleStatus(){
        return null;
    }
}
